package online.hxstart.code.concurrent.chapter08;

/**
 * @author huxing
 * @date 2019/7/11 星期四 14:17
 * @describe 用于线程池内部
 */
public class InternalTask implements Runnable {


  private final RunnableQueue runnableQueue;

  private volatile boolean running = true;


  public InternalTask(RunnableQueue runnableQueue) {
    this.runnableQueue = runnableQueue;
  }

  @Override
  public void run() {

    while (running && !Thread.currentThread().isInterrupted()) {

      try {
        Runnable task = runnableQueue.take();
        task.run();
      } catch (Exception e) {
        running = false;
        break;
      }
    }
  }

  public void stop() {
    this.running = false;
  }
}
